package com.easemob.chat;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EMLog;
import java.util.Set;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.packet.Presence.Type;

class EMPresenceHandler
{
  private static final String TAG = EMPresenceHandler.class.getSimpleName();
  private static EMPresenceHandler me = new EMPresenceHandler();

  static EMPresenceHandler getInstance()
  {
    return me;
  }

  void acceptInvitation(String paramString, boolean paramBoolean)
    throws EaseMobException
  {
    try
    {
      EMSessionManager.getInstance().checkConnection();
      Presence localPresence1 = new Presence(Presence.Type.subscribed);
      localPresence1.setMode(Presence.Mode.available);
      localPresence1.setPriority(24);
      localPresence1.setTo(paramString);
      localPresence1.setStatus("[resp:" + paramBoolean + "]");
      EMSessionManager.getInstance().getConnection().sendPacket(localPresence1);
      if (paramBoolean)
      {
        Presence localPresence2 = new Presence(Presence.Type.subscribe);
        localPresence2.setStatus("[resp:true]");
        localPresence2.setTo(paramString);
        EMSessionManager.getInstance().getConnection().sendPacket(localPresence2);
      }
    }
    catch (Exception localException)
    {
      throw new EaseMobException(localException.getMessage());
    }
  }

  void refuseInvitation(String paramString)
    throws EaseMobException
  {
    try
    {
      EMSessionManager.getInstance().checkConnection();
      Presence localPresence = new Presence(Presence.Type.unsubscribed);
      localPresence.setTo(EMContactManager.getEidFromUserName(paramString));
      EMSessionManager.getInstance().getConnection().sendPacket(localPresence);
    }
    catch (Exception localException)
    {
      throw new EaseMobException(localException.getMessage());
    }
  }

  void processRosterPresence(Presence paramPresence)
  {
    int i = 0;
    Object localObject1;
    String str2;
    Object localObject3;
    if (paramPresence.getType().equals(Presence.Type.subscribe))
    {
      localObject1 = null;
      boolean bool2 = false;
      if (paramPresence.getStatus() != null)
        localObject1 = paramPresence.getStatus();
      str2 = "[resp:";
      Object localObject2;
      if ((!TextUtils.isEmpty((CharSequence)localObject1)) && (((String)localObject1).startsWith(str2)))
      {
        localObject2 = ((String)localObject1).substring(str2.length(), ((String)localObject1).indexOf("]"));
        bool2 = Boolean.parseBoolean((String)localObject2);
        if (((String)localObject1).length() > ((String)localObject1).indexOf("]") + 1)
          localObject1 = ((String)localObject1).substring(((String)localObject1).indexOf("]1"), ((String)localObject1).length());
        else
          localObject1 = null;
      }
      EMLog.d(TAG, "isresp:" + bool2 + " reason:" + (String)localObject1);
      if (bool2)
      {
        try
        {
          acceptInvitation(paramPresence.getFrom(), false);
          if (i != 0)
            return;
          if (bool2)
          {
            localObject2 = new Intent(EMChatManager.getInstance().getContactInviteEventBroadcastAction());
            ((Intent)localObject2).putExtra("username", EMContactManager.getUserNameFromEid(paramPresence.getFrom()));
            ((Intent)localObject2).putExtra("isResponse", bool2);
            EMChat.getInstance().getAppContext().sendBroadcast((Intent)localObject2);
            if (EMContactManager.getInstance().contactListener != null)
              EMContactManager.getInstance().contactListener.onContactAgreed(EMContactManager.getUserNameFromEid(paramPresence.getFrom()));
          }
          i = 1;
        }
        catch (EaseMobException localEaseMobException1)
        {
          EMLog.e(TAG, localEaseMobException1.getMessage());
          localEaseMobException1.printStackTrace();
        }
      }
      else
      {
        if (EMChatManager.getInstance().getChatOptions().getAcceptInvitationAlways())
        {
          try
          {
            EMLog.d(TAG, "auto acceptance inviation from:" + paramPresence.getFrom());
            acceptInvitation(paramPresence.getFrom(), true);
          }
          catch (EaseMobException localEaseMobException2)
          {
            EMLog.e(TAG, localEaseMobException2.getMessage());
            localEaseMobException2.printStackTrace();
          }
          return;
        }
        localObject3 = EMContactManager.getUserNameFromEid(paramPresence.getFrom());
        Intent localIntent = new Intent(EMChatManager.getInstance().getContactInviteEventBroadcastAction());
        localIntent.putExtra("username", (String)localObject3);
        localIntent.putExtra("reason", (String)localObject1);
        localIntent.putExtra("isResponse", bool2);
        EMChat.getInstance().getAppContext().sendOrderedBroadcast(localIntent, null);
        EMLog.d(TAG, "send roster broadcast username:" + (String)localObject3 + " reason:" + (String)localObject1 + "resp:" + bool2);
        EMContactManager.getInstance().contactListener.onContactInvited((String)localObject3, (String)localObject1);
      }
    }
    else if (paramPresence.getType().equals(Presence.Type.unsubscribe))
    {
      EMContactManager.getInstance().deleteContactsSet.add(paramPresence.getFrom());
      localObject1 = new Presence(Presence.Type.unsubscribed);
      ((Presence)localObject1).setMode(Presence.Mode.available);
      ((Presence)localObject1).setPriority(24);
      ((Presence)localObject1).setTo(paramPresence.getFrom());
      EMSessionManager.getInstance().getConnection().sendPacket((Packet)localObject1);
    }
    else if ((paramPresence.getType().equals(Presence.Type.subscribed)) && (i == 0))
    {
      boolean bool1 = false;
      String str1 = null;
      if (paramPresence.getStatus() != null)
        str1 = paramPresence.getStatus();
      str2 = "[resp:";
      if ((str1 != null) && (str1.startsWith(str2)))
      {
        localObject3 = str1.substring(str2.length(), str1.indexOf("]"));
        bool1 = Boolean.parseBoolean((String)localObject3);
      }
      if (bool1)
      {
        localObject3 = new Intent(EMChatManager.getInstance().getContactInviteEventBroadcastAction());
        ((Intent)localObject3).putExtra("username", EMContactManager.getUserNameFromEid(paramPresence.getFrom()));
        ((Intent)localObject3).putExtra("isResponse", bool1);
        EMChat.getInstance().getAppContext().sendBroadcast((Intent)localObject3);
        EMContactManager.getInstance().contactListener.onContactAgreed(EMContactManager.getUserNameFromEid(paramPresence.getFrom()));
      }
      i = 1;
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMPresenceHandler
 * JD-Core Version:    0.6.1
 */